"""
面板显示调试工具 - 专门分析面板显示和隐藏的问题
"""
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QDesktopWidget
from PyQt5.QtCore import Qt, QTimer
from app_launcher_fluent import ShortcutsPanel


class PanelDebugTool(QMainWindow):
    """面板显示调试工具"""
    
    def __init__(self):
        super().__init__()
        self.setWindowTitle("面板显示调试工具")
        self.setGeometry(100, 100, 400, 500)
        
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建布局
        layout = QVBoxLayout(central_widget)
        
        # 创建面板实例
        self.panel = None
        
        # 创建控制按钮
        self.create_btn = QPushButton("1. 创建面板")
        self.create_btn.clicked.connect(self.create_panel)
        layout.addWidget(self.create_btn)
        
        self.move_btn = QPushButton("2. 移动面板到右侧")
        self.move_btn.clicked.connect(self.move_panel)
        layout.addWidget(self.move_btn)
        
        self.show_btn = QPushButton("3. 显示面板 (show)")
        self.show_btn.clicked.connect(self.show_panel)
        layout.addWidget(self.show_btn)
        
        self.visible_btn = QPushButton("4. 设置面板可见 (setVisible)")
        self.visible_btn.clicked.connect(self.set_visible)
        layout.addWidget(self.visible_btn)
        
        self.raise_btn = QPushButton("5. 提升面板 (raise)")
        self.raise_btn.clicked.connect(self.raise_panel)
        layout.addWidget(self.raise_btn)
        
        self.activate_btn = QPushButton("6. 激活面板 (activateWindow)")
        self.activate_btn.clicked.connect(self.activate_panel)
        layout.addWidget(self.activate_btn)
        
        self.anim_btn = QPushButton("7. 运行淡入动画")
        self.anim_btn.clicked.connect(self.run_animation)
        layout.addWidget(self.anim_btn)
        
        self.hide_btn = QPushButton("8. 隐藏面板 (hide)")
        self.hide_btn.clicked.connect(self.hide_panel)
        layout.addWidget(self.hide_btn)
        
        self.toggle_btn = QPushButton("9. 切换面板 (show/hide)")
        self.toggle_btn.clicked.connect(self.toggle_panel)
        layout.addWidget(self.toggle_btn)
        
        # 最后显示状态
        self.status_btn = QPushButton("检查面板状态")
        self.status_btn.clicked.connect(self.check_status)
        layout.addWidget(self.status_btn)
        
    def create_panel(self):
        """创建面板"""
        try:
            print("创建面板...")
            self.panel = ShortcutsPanel()
            print("面板已创建")
            self.check_status()
        except Exception as e:
            print(f"创建面板时出错: {e}")
            import traceback
            traceback.print_exc()
    
    def move_panel(self):
        """移动面板"""
        if not self.panel:
            print("面板未创建，请先创建面板")
            return
            
        try:
            screen = QDesktopWidget().screenGeometry()
            target_x = screen.width() - self.panel.width() - 50
            target_y = (screen.height() - self.panel.height()) // 2
            
            print(f"移动面板到: ({target_x}, {target_y})")
            self.panel.move(target_x, target_y)
            print(f"面板位置现在是: ({self.panel.x()}, {self.panel.y()})")
        except Exception as e:
            print(f"移动面板时出错: {e}")
    
    def show_panel(self):
        """显示面板"""
        if not self.panel:
            print("面板未创建，请先创建面板")
            return
            
        try:
            print("调用panel.show()...")
            self.panel.show()
            print("panel.show()已调用")
            self.check_status()
        except Exception as e:
            print(f"显示面板时出错: {e}")
    
    def set_visible(self):
        """设置面板可见"""
        if not self.panel:
            print("面板未创建，请先创建面板")
            return
            
        try:
            print("调用panel.setVisible(True)...")
            self.panel.setVisible(True)
            print("panel.setVisible(True)已调用")
            self.check_status()
        except Exception as e:
            print(f"设置面板可见时出错: {e}")
    
    def raise_panel(self):
        """提升面板"""
        if not self.panel:
            print("面板未创建，请先创建面板")
            return
            
        try:
            print("调用panel.raise_()...")
            self.panel.raise_()
            print("panel.raise_()已调用")
        except Exception as e:
            print(f"提升面板时出错: {e}")
    
    def activate_panel(self):
        """激活面板"""
        if not self.panel:
            print("面板未创建，请先创建面板")
            return
            
        try:
            print("调用panel.activateWindow()...")
            self.panel.activateWindow()
            print("panel.activateWindow()已调用")
        except Exception as e:
            print(f"激活面板时出错: {e}")
    
    def run_animation(self):
        """运行淡入动画"""
        if not self.panel:
            print("面板未创建，请先创建面板")
            return
            
        try:
            print("调用panel.fadeInAnimation()...")
            self.panel.fadeInAnimation()
            print("panel.fadeInAnimation()已调用")
            self.check_status()
        except Exception as e:
            print(f"运行淡入动画时出错: {e}")
    
    def hide_panel(self):
        """隐藏面板"""
        if not self.panel:
            print("面板未创建，请先创建面板")
            return
            
        try:
            print("调用panel.hide()...")
            self.panel.hide()
            print("panel.hide()已调用")
            self.check_status()
        except Exception as e:
            print(f"隐藏面板时出错: {e}")
    
    def toggle_panel(self):
        """切换面板"""
        if not self.panel:
            print("面板未创建，请先创建面板")
            self.create_panel()
            QTimer.singleShot(500, self.show_panel)
            return
            
        try:
            if self.panel.isVisible():
                print("面板可见，切换为隐藏")
                self.panel.hide()
            else:
                print("面板不可见，切换为显示")
                self.panel.show()
                self.panel.raise_()
                self.panel.activateWindow()
            
            self.check_status()
        except Exception as e:
            print(f"切换面板时出错: {e}")
    
    def check_status(self):
        """检查面板状态"""
        if not self.panel:
            print("面板未创建")
            return
            
        try:
            visible = self.panel.isVisible()
            opacity = self.panel.windowOpacity()
            flags = self.panel.windowFlags()
            
            print("\n----- 面板状态 -----")
            print(f"可见性: {'可见' if visible else '隐藏'}")
            print(f"不透明度: {opacity:.2f}")
            print(f"位置: ({self.panel.x()}, {self.panel.y()})")
            print(f"大小: {self.panel.width()} x {self.panel.height()}")
            print(f"窗口标志: {flags}")
            print("-------------------\n")
            
            self.status_btn.setText(f"面板状态: {'可见' if visible else '隐藏'}")
        except Exception as e:
            print(f"检查状态时出错: {e}")
            self.status_btn.setText("检查状态出错")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = PanelDebugTool()
    window.show()
    sys.exit(app.exec_())
